package com.qc31.monitorlib.ui.video.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.qc31.monitorlib.R
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView

/** @Author Jasper @Time 2020/12/1 @Desc todo */
class ControlVideo : StandardGSYVideoPlayer {
    //是否需要静音
    private var isNeedMute = true
    private lateinit var ivSwitchVoice: ImageView
    override fun getLayoutId(): Int {
        return R.layout.control_video
    }

    constructor(context: Context?, fullFlag: Boolean?) : super(context, fullFlag)
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    override fun init(context: Context?) {
        super.init(context)
        ivSwitchVoice = findViewById(R.id.ivSwitchVoice)
        initView()
    }

    fun hiedView() {
        setViewShowState(mTopContainer, INVISIBLE)
    }

    private fun initView() {
        ivSwitchVoice.setOnClickListener {
            isNeedMute = !isNeedMute
            GSYVideoManager.instance().isNeedMute = isNeedMute
            resolverVoice()
        }
    }

    fun initVoice() {
        isNeedMute = true
        GSYVideoManager.instance().isNeedMute = true
        resolverVoice()
    }

    override fun updateStartImage() {
        if (mStartButton == null) return
        if (mStartButton is ImageView) {
            val imageView = mStartButton as ImageView
            when (mCurrentState) {
                GSYVideoView.CURRENT_STATE_PLAYING -> {
                    imageView.setImageResource(R.mipmap.video_pause_pressed)
                }
                GSYVideoView.CURRENT_STATE_ERROR -> {
                    imageView.setImageResource(R.mipmap.video_play_normal)
                }
                else -> {
                    imageView.setImageResource(R.mipmap.video_play_normal)
                }
            }
        }
    }

    override fun startWindowFullscreen(
        context: Context?, actionBar: Boolean, statusBar: Boolean
    ): GSYBaseVideoPlayer {
        val controlVideo =
            super.startWindowFullscreen(context, actionBar, statusBar) as ControlVideo
        controlVideo.isNeedMute = isNeedMute
        controlVideo.resolverVoice()
        return controlVideo
    }

    override fun resolveNormalVideoShow(
        oldF: View?, vp: ViewGroup?, gsyVideoPlayer: GSYVideoPlayer?
    ) {
        super.resolveNormalVideoShow(oldF, vp, gsyVideoPlayer)
        if (gsyVideoPlayer != null) {
            val controlVideo = gsyVideoPlayer as ControlVideo
            isNeedMute = controlVideo.isNeedMute
            resolverVoice()
        }

    }


    //声音
    private fun resolverVoice() {
        val id: Int = if (isNeedMute) {
            R.mipmap.ic_voice_close
        } else {
            R.mipmap.ic_voice_open
        }
        ivSwitchVoice.setImageResource(id)
    }

}